package com.mycat.chat.two.ui;

import com.mycat.chat.two.msg.MessageLogin;
import com.mycat.chat.two.msg.base.MessageType;
import com.mycat.chat.two.socket.client.ChatClient;
import com.mycat.chat.two.util.Constants;
import com.mycat.chat.two.util.PictureUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 登陆窗口
 *
 * @author roger.
 * @email 190642964@qq.com
 * @create 2017-02-16
 */
public class LoginWindow extends JFrame {

    /**
     * 主面板
     */
    private JPanel content;
    /**
     * 最小化按钮
     */
    private JLabel minButton;
    /**
     * 退出按钮
     */
    private JLabel exitButton;
    /**
     * 头像
     */
    private JLabel pictureLabel;
    /**
     * 账号
     */
    private JTextField userNameField;
    /**
     * 密码
     */
    private JPasswordField passWordField;
    /**
     * 保存密码Box
     */
    private JLabel savePassCheckBox;
    /**
     * 保存密码Txt
     */
    private JLabel savePassLabel;
    /**
     * 自动登录Box
     */
    private JLabel autoLoginCheckBox;
    /**
     * 自动登录Txt
     */
    private JLabel autoLoginLabel;
    /**
     * 登陆按钮
     */
    private JLabel loginButton;
    /**
     * 注册账号
     */
    private JLabel registerLabel;
    /**
     * 找回密码
     */
    private JLabel findPassLabel;
    /**
     * 坐标（拖动记录）
     */
    private Point point = new Point();
    /**
     * 是否自动登陆
     */
    private boolean isAutoLogin;
    /**
     * 是否保存密码
     */
    private boolean isSavePass;

    private ChatClient chatClient;

    public LoginWindow(ChatClient chatClient) {
        super();
        this.chatClient = chatClient;
        initUI();
        initListener();

        // 设置为屏幕居中
        this.setLocationRelativeTo(null);
        // 启动窗口是在最顶层
        this.setVisible(true);
    }

    /**
     * 初始化页面UI
     */
    public void initUI() {
        // 设置窗口大小
        setSize(397, 300);
        // 取消窗口的修饰框
        setUndecorated(true);
        // 用户单击关闭按钮时的程序执行操作，销毁
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        // 主面板
        content = new JPanel() {
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(PictureUtil.getPicture("/login/login_back.jpg").getImage(), 0, 0, null);
                this.setOpaque(false);
            }
        };
        content.setLayout(null);
        // 创建Jpanel的边框
        content.setBorder(Constants.GRAY_BORDER);
        // 将Jpanel设置为居中
        getContentPane().add(content, BorderLayout.CENTER);

        // 头像区域
        pictureLabel = new JLabel();
        content.add(pictureLabel);
        pictureLabel.setBounds(21, 125, 90, 90);
        pictureLabel.setBorder(Constants.LIGHT_GRAY_BORDER);
        pictureLabel.setIcon(PictureUtil.getPicture("/login/header_img.png"));

        // 账号
        userNameField = new JTextField();
        content.add(userNameField);
        userNameField.setBounds(121, 126, 183, 30);
        userNameField.setBorder(Constants.LIGHT_GRAY_BORDER);

        // 密码
        passWordField = new JPasswordField();
        content.add(passWordField);
        passWordField.setBounds(121, 158, 183, 30);
        passWordField.setBorder(Constants.LIGHT_GRAY_BORDER);

        // 最小化按钮
        minButton = new JLabel();
        content.add(minButton);
        minButton.setBounds(330, 0, 30, 20);
        minButton.setIcon(PictureUtil.getPicture("/common/minimize.png"));

        // 关闭按钮
        exitButton = new JLabel();
        content.add(exitButton);
        exitButton.setBounds(358, 0, 40, 20);
        exitButton.setIcon(PictureUtil.getPicture("/common/close.png"));

        // 保存密码Box
        savePassCheckBox = new JLabel();
        content.add(savePassCheckBox);
        savePassCheckBox.setBounds(121, 195, 20, 22);
        savePassCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));

        // 保存密码Txt
        savePassLabel = new JLabel("保存密码");
        content.add(savePassLabel);
        savePassLabel.setFont(Constants.BASIC_FONT);
        savePassLabel.setBounds(147, 192, 54, 24);

        // 自动登录Box
        autoLoginCheckBox = new JLabel();
        content.add(autoLoginCheckBox);
        autoLoginCheckBox.setBounds(226, 195, 20, 22);
        autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));

        // 自动登录Txt
        autoLoginLabel = new JLabel("自动登录");
        content.add(autoLoginLabel);
        autoLoginLabel.setFont(Constants.BASIC_FONT);
        autoLoginLabel.setBounds(251, 192, 52, 24);

        // 登陆按钮
        loginButton = new JLabel();
        content.add(loginButton);
        loginButton.setBounds(118, 238, 186, 40);
        loginButton.setIcon(PictureUtil.getPicture("/login/login_button.png"));

        // 注册账号
        registerLabel = new JLabel("注册账号");
        content.add(registerLabel);
        registerLabel.setFont(Constants.BASIC_FONT);
        registerLabel.setBounds(314, 129, 62, 24);

        // 找回密码
        findPassLabel = new JLabel("找回密码");
        content.add(findPassLabel);
        findPassLabel.setFont(Constants.BASIC_FONT);
        findPassLabel.setBounds(314, 160, 62, 27);
    }

    /**
     * 初始化各个标签的事件监听
     */
    private void initListener() {
        // 主窗体事件
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                point.x = e.getX();
                point.y = e.getY();
            }
        });
        // 设置窗口移动监听
        this.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                Point p = getLocation();
                setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
            }
        });
        // 最小化按钮事件
        minButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                minButton.setIcon(PictureUtil.getPicture("/common/minimize.png"));
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                minButton.setIcon(PictureUtil.getPicture("/common/minimize_active.png"));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                setVisible(false);
            }
        });
        // 退出按钮事件
        exitButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                exitButton.setIcon(PictureUtil.getPicture("/common/close.png"));
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                exitButton.setIcon(PictureUtil.getPicture("/common/close_active.png"));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
//                tray.remove(icon);
                System.exit(0);
            }
        });
        // 账号输入框焦点事件
        userNameField.addFocusListener(new FocusListener() {
            @Override
            public void focusLost(FocusEvent e) {
                userNameField.setBorder(Constants.LIGHT_GRAY_BORDER);
            }

            @Override
            public void focusGained(FocusEvent e) {
                userNameField.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
            }
        });
        // 键盘事件
        userNameField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 键盘为回车键
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    System.out.println("账号框回车键.....登陆");
                    login();
                }
            }
        });
        // 密码输入框焦点事件
        passWordField.addFocusListener(new FocusListener() {
            @Override
            public void focusLost(FocusEvent e) {
                passWordField.setBorder(Constants.LIGHT_GRAY_BORDER);
            }

            @Override
            public void focusGained(FocusEvent e) {
                passWordField.setBorder(Constants.ORANGE_BORDER);
            }
        });
        // 键盘事件
        passWordField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                // 键盘为回车键
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    System.out.println("密码框回车键.....登陆");
                    login();
                }
            }
        });
        // 保存密码复选框事件
        savePassCheckBox.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isSavePass) {
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isSavePass = false;
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isAutoLogin = false;
                } else {
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isSavePass = true;
                }
            }
        });
        // 保存密码字体事件
        savePassLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isSavePass) {
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isSavePass = false;
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isAutoLogin = false;
                } else {
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isSavePass = true;
                }
            }
        });
        // 自动登录复选框事件
        autoLoginCheckBox.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isAutoLogin) {
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isAutoLogin = false;
                } else {
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isAutoLogin = true;
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isSavePass = true;
                }
            }
        });
        // 自动登录字体事件
        autoLoginLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (isAutoLogin) {
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/no_select.png"));
                    isAutoLogin = false;
                } else {
                    autoLoginCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isAutoLogin = true;
                    savePassCheckBox.setIcon(PictureUtil.getPicture("/login/selected.png"));
                    isSavePass = true;
                }
            }
        });
        // 登陆按钮事件
        loginButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                loginButton.setIcon(PictureUtil.getPicture("/login/login_button.png"));
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                loginButton.setIcon(PictureUtil.getPicture("/login/login_active.png"));
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                System.out.println("登陆键.....登陆");
                login();
            }
        });
        // 注册账号鼠标事件
        registerLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                registerLabel.setText("\u6ce8\u518c\u8d26\u53f7");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                registerLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                registerLabel.setText("<html><u>\u6ce8\u518c\u8d26\u53f7</u><html>");
            }

            @Override
            public void mouseClicked(MouseEvent e) {

            }
        });
        // 找回密码鼠标事件
        findPassLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseExited(MouseEvent e) {
                findPassLabel.setText("\u627e\u56de\u5bc6\u7801");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                findPassLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
                findPassLabel.setText("<html><u>\u627e\u56de\u5bc6\u7801</u><html>");
            }
        });
    }

    public void login() {
        String userCode = userNameField.getText();
        String password = String.valueOf(passWordField.getPassword());
        MessageLogin messageLogin = new MessageLogin(MessageType.LOGIN_MSG.getType(),userCode,password);
    }

}
